package baiwa.rta.utils;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import baiwa.rta.model.JQueryDataTableParamModel;

public class DataTablesParamUtility {

	public static <T> JQueryDataTableParamModel<T> getParam(HttpServletRequest request) {

		if (request.getParameter("sEcho") != null && request.getParameter("sEcho") != "") {
			JQueryDataTableParamModel<T> param = new JQueryDataTableParamModel<>();
			param.sEcho = request.getParameter("sEcho");
			param.sSearch = request.getParameter("sSearch");
			param.sColumns = request.getParameter("sColumns");
			param.iDisplayStart = getIntParam(request, "iDisplayStart");
			param.iDisplayLength = getIntParam(request, "iDisplayLength");
			param.iColumns = getIntParam(request, "iColumns");
			param.iSortingCols = getIntParam(request, "iSortingCols");
			param.iSortColumnIndex = getIntParam(request, "iSortCol_0");
			param.sSortDirection = request.getParameter("sSortDir_0");
			param.items = new ArrayList<>();
			return param;
		} else
			return new JQueryDataTableParamModel<T>();
	}

	private static Integer getIntParam(final HttpServletRequest request, final String paramName) {
		String o = request.getParameter(paramName);
		return Integer.parseInt(o == null ? "0" : o);
	}
}